require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker',
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;


            this.collection = new Collection({
                url: _global.url.api + 'wechatred_manage/' + id,
                condition: {limit: 10},  //查询条件
                model: {
                    url: _global.url.api + 'wechatred_manage/' + id,
                    event: {
                        '.item-remove:click': 'remove',
                        '.item-edit:click': 'edit',
                        '.item-stop:click': 'stop',
                        '.item-start:click': 'start'
                    },

                    stop: function (e) {
                        e.preventDefault();
                        var _this = this;
                        _this.data = this.data;
                        $.ajax({
                            url: _global.url.api + 'wechatred_manage/put/' + id + '?status=0&raffle_reward_id=' + this.get_data('id'),
                            type: 'put',
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.status = 0;
                                    _this.create_element();
                                } else if (res.code == 10001) {
                                    e.preventDefault();

                                    dialog({
                                        title: '停用奖品',
                                        content: '确定停用吗?只剩一个奖品了',
                                        width: 700,
                                        quickClose: true,
                                        ok: function () {
                                            if (new Model({
                                                    url: _global.url.api + 'wechatred_manage/stop/' + id ,
                                                    data: _this.data
                                                }).put()) {
                                            }
                                            window.location.reload();
                                            //_this.collection.reload();
                                        }, cancel: function () {
                                        }
                                    }).show();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },

                    start: function (e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'wechatred_manage/put/' + id + '?status=1&raffle_reward_id=' + this.get_data('id'),
                            type: 'put',
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.status = 1;
                                    _this.create_element();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },

                    remove: function (e) {
                        e.preventDefault();
                        var _this = this;
                        _this.data = this.data;
                        $.ajax({
                            url: _global.url.api + 'wechatred_manage/' + id + '/delete',
                            type: 'delete',
                            data: _this.data,
                            success: function (res) {
                                if (res.code == 0) {
                                    window.location.reload();
                                } else if (res.code == 10001) {
                                    e.preventDefault();
                                    dialog({
                                        title: '删除奖品',
                                        content: '确定删除吗?没有可用奖品了',
                                        width: 700,
                                        quickClose: true,
                                        ok: function () {
                                            if (new Model({
                                                    url: _global.url.api + 'wechatred_manage/' + id + '/delete_confirm',
                                                    data: _this.data
                                                }).delete()) {
                                            }
                                            window.location.reload();
                                            //_this.collection.reload();
                                        }, cancel: function () {
                                        }
                                    }).show();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },
                    edit: function (e) {
                        data = {}
                        e.preventDefault();
                        _this.data = this.data;
                        dialog({
                            title: '编辑活动',
                            content: template('edit-tpl', this.data),
                            width: 700,
                            quickClose: true,
                            ok: function () {
                                _this.data.count = $("input[name='count']").val();
                                if (_this.data.count == '' || _this.data.count == undefined) {
                                    Alert({msg: '请输入投放数量', type: 'error'});
                                    return false;
                                }
                                _this.data.reward_id = $("input[name='reward_id']").val();

                                if (new Model({
                                        url: _global.url.api + 'wechatred_manage/edit/' + id +  '?count=' + _this.data.count + '&reward_id=' + _this.data.reward_id,
                                        data: data
                                    }).put()) {
                                }
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show();
                    }
                },
                callback: function (result) {
                    result.rate.reward_rate_real && $('#btn-set').attr('data-rate-real', result.rate.reward_rate_real);
                    result.rate.reward_rate_virtual && $('#btn-set').attr('data-rate-vir', result.rate.reward_rate_virtual);
                    console.log(result);
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            this.data = {};
            var _this = this;

            //保存
            $('#btn-add-coupon').on('click', function (e) {
                data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加优惠券',
                    content: template('add-tpl', {}),
                    width: 700,
                    ok: function () {
                        _this.data.name = $("input[name='title']").val();
                        if (_this.data.name == '' || _this.data.name == undefined) {
                            Alert({msg: '请输入优惠券名称', type: 'error'});
                            return false;
                        }
                        _this.data.coupon_id = $("input[name='coupon_id']:checked").val();

                        if (_this.data.coupon_id == '' || _this.data.coupon_id == undefined) {
                            Alert({msg: '请选择优惠券', type: 'error'});
                            return false;
                        }
                        _this.data.count = $("input[name='count']").val();
                        if (_this.data.count == '' || _this.data.count == undefined) {
                            Alert({msg: '请输入投放数量', type: 'error'});
                            return false;
                        }
                        _this.data.image = $("input[name='imgs']").val();
                        if (_this.data.image == '' || _this.data.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }

                        console.log(data);
                        if (new Model({
                                url: _global.url.api + 'wechatred_manage/addvirtual/' + id,
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();
            });

            $('#btn-add-particular').on('click', function (e) {
                data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加实物',
                    content: template('add-tpl2', {}),
                    width: 700,
                    ok: function () {
                        _this.data.name = $("input[name='title']").val();
                        if (_this.data.name == '' || _this.data.name == undefined) {
                            Alert({msg: '请输入实物名称', type: 'error'});
                            return false;
                        }
                        _this.data.related_id = $("input[name='particular_good_id']:checked").val();
                        if (_this.data.related_id == '' || _this.data.related_id == undefined) {
                            Alert({msg: '请选择定向物品', type: 'error'});
                            return false;
                        }
                        _this.data.count = $("input[name='count']").val();
                        if (_this.data.count == '' || _this.data.count == undefined) {
                            Alert({msg: '请输入投放数量', type: 'error'});
                            return false;
                        }
                        _this.data.image = $("input[name='imgs']").val();
                        if (_this.data.image == '' || _this.data.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }

                        console.log(data);
                        if (new Model({
                                url: _global.url.api + 'wechatred_manage/addreal/' + id + '?related_id=' + _this.data.related_id + '&image=' + _this.data.image + '&count=' + _this.data.count + '&name=' + _this.data.name,
                                data: data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();

            });


            $('#btn-set').on('click', function (e) {
                data = {};
                e.preventDefault();
                var realRate = $(this).attr('data-rate-real') * 100;
                var virRate = $(this).attr('data-rate-vir') * 100;
                _this.dialog = dialog({
                    title: '设置中奖概率实物',
                    content: template('add-tpl3', {"vir": virRate, "real": realRate}),
                    width: 700,
                    ok: function () {
                        _this.data.reward_rate_virtual = $("input[name='reward_rate_virtual']").val() / 100;
                        //console.log(_this.data.reward_rate_virtual);
                        if (_this.data.reward_rate_virtual > 1 || _this.data.reward_rate_virtual < 0) {
                            Alert({msg: '请输入优惠券中奖概率(0-100之间)', type: 'error'});
                            return false;
                        }
                        _this.data.reward_rate_real = $("input[name='reward_rate_real']").val() / 100;
                        if (_this.data.reward_rate_real > 1 || _this.data.reward_rate_real < 0) {
                            Alert({msg: '请输入实物中奖概率(0-100之间)', type: 'error'});
                            return false;
                        }
                        if ((_this.data.reward_rate_virtual + _this.data.reward_rate_real) > 1) {
                            Alert({msg: '概率之和不能大于100', type: 'error'});
                            return false;
                        }


                        if (new Model({
                                url: _global.url.api + 'wechatred_manage/set/' + id + '?reward_rate_virtual=' + _this.data.reward_rate_virtual + '&reward_rate_real=' + _this.data.reward_rate_real,
                                data: data
                            }).put()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();

            });
            $('#btn-search').on('click', function () {
                window.location.href = '/admin/wechatred/' + id;
            });

            //上传图片
            $('body').on('click', '#btn-upload', function (e) {

                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        // console.log(123123123);
                        console.log('contentWindow', $("#upload-frame")[0].contentWindow.image);
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });


            $('body').on('click', '#btn-coupon-search', function (e) {
                console.log(111);
                var coupon = $('#keyword').val();
                $.ajax({
                    url: _global.url.api + 'wechatred_search_coupon_id',
                    data: {coupon: coupon, platform: 0},
                    type: 'get',
                    async: false,
                    success: function (res) {
                        if (res.code == 0) {
                            console.log(res.data);
                            var obj = eval(res.data);
                            // alert(obj.length);
                            var html = '';
                            for (var i = 0; i < obj.length; i++) {
                                html += "<tr><td>" + obj[i].coupon_name + "</td><td>" + obj[i].site_name + "</td><td><input type='radio' name='coupon_id' value='" + obj[i].id + "'/>操作</td></tr>"
                            }
                            // alert(html);
                            // alert($('#data-list1').html());
                            $('#data-list1').html(html);

                        } else {
                            Alert({msg: res.msg, type: 'error'});
                            return false;
                        }
                    }
                });
            });

            $('body').on('click', '#btn-goods-search-platform', function (e) {
                console.log(123);
                var coupon = $('#keyword').val();
                $.ajax({
                    url: _global.url.api + 'wechatred_search_coupon_id',
                    data: {coupon: coupon, platform: 1},
                    type: 'get',
                    async: false,
                    success: function (res) {
                        if (res.code == 0) {
                            console.log(res.data);
                            var obj = eval(res.data);
                            // alert(obj.length);
                            var html = '';
                            for (var i = 0; i < obj.length; i++) {
                                html += "<tr><td>" + obj[i].coupon_name + "</td><td>" + obj[i].site_name + "</td><td><input type='radio' name='coupon_id' value='" + obj[i].id + "'/>操作</td></tr>"
                            }
                            // alert(html);
                            // alert($('#data-list1').html());
                            $('#data-list1').html(html);

                        } else {
                            Alert({msg: res.msg, type: 'error'});
                            return false;
                        }
                    }
                });
            });
            $('body').on('click', '#btn-good-search', function (e) {
                console.log(111);
                var good = $('#keyword').val();
                $.ajax({
                    url: _global.url.api + 'wechatred_search_good_id/' + id,
                    data: {good: good},
                    type: 'get',
                    async: false,
                    success: function (res) {
                        if (res.code == 0) {
                            console.log(res.data);
                            var obj = eval(res.data);
                            if (obj.length < 1) {
                                alert('请前往定向定价添加用户标签是摇一摇活动名称的奖品');
                            }
                            var html = '';
                            for (var i = 0; i < obj.length; i++) {
                                html += "<tr><td>" + obj[i].goodName + "</td><td>" + obj[i].origin_price + "</td><td>" + obj[i].particular_price + "</td><td>" + obj[i].buy_count_limit +
                                "</td><td>" + obj[i].groupName + "</td><td><input type='radio' name='particular_good_id' value='" + obj[i].goodId + "'/>操作</td></tr>"
                            }
                            $('#data-list1').html(html);

                        } else {
                            Alert({msg: res.msg, type: 'error'});
                            return false;
                        }
                    }
                });
            });

        },


        // change: function (e) {
        //     this.data[$(e.target).attr('name')] = $(e.target).val();
        // },
        imgPreview: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id != 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }


    };

    new index();
});